package com.zby.service.imp;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zby.common.utils.JwtUtils;
import com.zby.common.utils.ResultEnum;
import com.zby.config.SecurityConfig;
import com.zby.common.exception.BlogException;
import com.zby.mapper.UserInfoMapper;
import com.zby.mapper.UserMapper;
import com.zby.model.pojo.User;
import com.zby.model.pojo.UserInfo;
import com.zby.model.vo.LoginUser;
import com.zby.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.Map;

@Service
@Slf4j
public class UserServiceimp implements UserService {
    @Autowired
    private JwtUtils jwtUtils;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserInfoMapper userInfoMapper;

    @Override
    public String login(LoginUser loginUser) {
        if (!StringUtils.hasText(loginUser.getUsername())) {
            throw new BlogException(ResultEnum.USERNAME_NOT_NULL);
        }
        if (!StringUtils.hasText(loginUser.getPassword())) {
            throw new BlogException(ResultEnum.PASSWORD_NOT_NULL);
        }
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.allEq(Map.of("username", loginUser.getUsername()));
        User dbUser = userMapper.selectOne(wrapper);
        if (dbUser == null || !SecurityConfig.passwordEncoder().matches(loginUser.getPassword(), dbUser.getPassword())) {
            throw new BlogException(ResultEnum.U_P_ERROR);
        }
        return jwtUtils.createToken(Integer.toString(dbUser.getId()));
    }

    @Override
    @Transactional
    public void register(User user) {
        if (user.getUsername() == null || user.getUsername().isEmpty()) {
            throw new BlogException(ResultEnum.USERNAME_NOT_NULL);
        }
        if (user.getPassword() == null || user.getPassword().isEmpty()) {
            throw new BlogException(ResultEnum.PASSWORD_NOT_NULL);
        }
        // 判断用户名是否重复
        User dbUser = userMapper.selectOne(new QueryWrapper<User>().eq("username", user.getUsername()));
        if (!ObjectUtils.isEmpty(dbUser)) {
            throw new BlogException(ResultEnum.USERNAME_DUPLICATE_ERROR);
        }
        user.setPassword(SecurityConfig.passwordEncoder().encode(user.getPassword()));
        userMapper.insert(user);
        log.info("ss{}", user.getId());
        if (user.getId() != 0) {
            UserInfo userInfo = new UserInfo();
            userInfo.setUserId(user.getId());
            userInfoMapper.insert(userInfo);
        }
    }

    @Override
    public String refreshToken(String refreshToken) {
        if (!StringUtils.hasText(refreshToken)) {
            throw new BlogException(ResultEnum.TOKEN_NOT_NULL);
        }
        return jwtUtils.createLongTermToken(refreshToken);
    }
}
